package com.aric.springbootmusic.service.impl;

import com.aric.springbootmusic.entity.dto.AddMusicDTO;
import com.aric.springbootmusic.entity.dto.EditMusicDTO;
import com.aric.springbootmusic.entity.param.FindMusicParam;
import com.aric.springbootmusic.entity.param.QueryAllMusicParam;
import com.aric.springbootmusic.entity.pojo.Music;
import com.aric.springbootmusic.entity.vo.*;
import com.aric.springbootmusic.exceptions.NameDuplicationException;
import com.aric.springbootmusic.mapper.MusicMapper;
import com.aric.springbootmusic.service.MusicService;
import com.aric.springbootmusic.utils.Result;
import com.aric.springbootmusic.utils.ResultUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Transactional //开启事务
public class MusicServiceImpl extends ServiceImpl<MusicMapper, Music> implements MusicService {
    @Autowired
    private MusicMapper musicMapper;
    //这里需要改写一下，就是展示的字段
    @Override
    public Result findAllMusicByQueryCondition(FindMusicParam findMusicParam) {
        String keyword = findMusicParam.getKeyword();
        Integer currentPage = findMusicParam.getCurrentPage();
        Integer pageSize = findMusicParam.getPageSize();

        Page<Music> queryPage=new Page<>(currentPage,pageSize);
        QueryWrapper<Music> queryWrapper=new QueryWrapper<>();
        QueryWrapper<Music> wrapper = queryWrapper.like("songName", keyword).or().like("singer", keyword);
        Page<Music> musicPage = musicMapper.selectPage(queryPage, wrapper);
        if(Objects.nonNull(musicPage)){
            List<Music> musicList = musicPage.getRecords();
            long total = musicPage.getTotal();
            List<ShowMusicVO> list=new ArrayList<>();
            musicList.forEach(music -> {
                ShowMusicVO build = ShowMusicVO.builder()
                        .id(music.getId())
                        .singer(music.getSinger())
                        .songName(music.getSongName())
                        .songType(music.getSongType().getCode())
                        .totalTime(music.getTotalTime())
                        .build();
                list.add(build);
            });

            MusicVO musicVO = MusicVO.builder().total(total).music(list).build();
            return ResultUtil.getResponseResult(HttpStatus.OK.value(),"查询成功！",musicVO);
        }
        return ResultUtil.getResponseResult(HttpStatus.OK.value(),"没有查询到数据",null);
    }

    /**
     * 添加音乐数据
     * @param musicDTO
     * @return
     */
    @Override
    public Result addSingleMusic(AddMusicDTO musicDTO) throws NameDuplicationException {
        //重名处理
        LambdaQueryWrapper<Music> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Music::getSongName,musicDTO.getSongName());
        Long count = musicMapper.selectCount(queryWrapper);

        if(count>0){
            //抛出重名异常
            throw new NameDuplicationException("已经存在相同的歌名");
        }
        //添加数据
        Music music = Music.builder()
                .songName(musicDTO.getSongName())
                .songAddress(musicDTO.getSongAddress())
                .songRealAddress(musicDTO.getSongRealAddress())
                .singer(musicDTO.getSinger())
                .lyric(musicDTO.getLyric())
                .picAddress(musicDTO.getPicAddress())
                .picRealAddress(musicDTO.getPicRealAddress())
                .totalTime(musicDTO.getTotalTime())
                .songType(musicDTO.getSongType())
                .isDelete(musicDTO.getIsDelete())
                .contentType(musicDTO.getContentType())
                .format(musicDTO.getFormat())
                .lyricist(musicDTO.getLyricist())
                .composer(musicDTO.getComposer())
                .arranger(musicDTO.getArranger())
                .tempo(musicDTO.getTempo())
                .remixer(musicDTO.getRemixer())
                .album(musicDTO.getAlbum())
                .copyright(musicDTO.getCopyright())
                .albumArtist(musicDTO.getAlbumArtist())
                .releaseDate(musicDTO.getReleaseDate())
                .releaseCountry(musicDTO.getReleaseCountry())
                .build();
        int i = musicMapper.insert(music);

        return i>0 ? ResultUtil.getResponseResult(HttpStatus.OK.value(), "添加成功!",null) : ResultUtil.getResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "添加失败!",null);
    }

    @Override
    public Result removeMusicById(String id) {
        int i = musicMapper.deleteById(id);
        return i>0 ? ResultUtil.getResponseResult(HttpStatus.OK.value(), "删除成功!",null) : ResultUtil.getResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败!",null);
    }

    @Override
    public Result findSingleMusic(String id) {
        Music music = musicMapper.selectById(id);
        if(Objects.nonNull(music)){
            ShowMusicInfo musicInfo = ShowMusicInfo.builder()
                    .id(music.getId())
                    .songName(music.getSongName())
                    .singer(music.getSinger())
                    .lyric(music.getLyric())
                    .songType(String.valueOf(music.getSongType().getCode()))
                    .picAddress(music.getPicAddress())
                    .picRealAddress(music.getPicRealAddress())
                    .songAddress(music.getSongAddress())
                    .songRealAddress(music.getSongRealAddress())
                    .totalTime(music.getTotalTime())
                    .contentType(music.getContentType())
                    .lyricist(music.getLyricist())
                    .composer(music.getComposer())
                    .arranger(music.getArranger())
                    .tempo(music.getTempo())
                    .remixer(music.getRemixer())
                    .album(music.getAlbum())
                    .albumArtist(music.getAlbumArtist())
                    .copyright(music.getCopyright())
                    .format(music.getFormat())
                    .releaseDate(music.getReleaseDate())
                    .releaseCountry(music.getReleaseCountry())
                    .build();
            return ResultUtil.getResponseResult(HttpStatus.OK.value(), "查询成功！",musicInfo);
        }
        return ResultUtil.getResponseResult(HttpStatus.OK.value(), "没有查询到任何数据！",null);
    }

    @Override
    public Result modifyMusic(EditMusicDTO musicDTO) {
        if(Objects.nonNull(musicDTO)){
            Music music = Music.builder()
                    .id(musicDTO.getId())
                    .songName(musicDTO.getSongName())
                    .singer(musicDTO.getSinger())
                    .lyric(musicDTO.getLyric())
                    .songAddress(musicDTO.getSongAddress())
                    .songRealAddress(musicDTO.getSongRealAddress())
                    .picAddress(musicDTO.getPicAddress())
                    .picRealAddress(musicDTO.getPicRealAddress())
                    .totalTime(musicDTO.getTotalTime())
                    .songType(musicDTO.getSongType())
                    .contentType(musicDTO.getContentType())
                    .format(musicDTO.getFormat())
                    .lyricist(musicDTO.getLyricist())
                    .composer(musicDTO.getComposer())
                    .arranger(musicDTO.getArranger())
                    .tempo(musicDTO.getTempo())
                    .remixer(musicDTO.getRemixer())
                    .album(musicDTO.getAlbum())
                    .albumArtist(musicDTO.getAlbumArtist())
                    .copyright(musicDTO.getCopyright())
                    .releaseDate(musicDTO.getReleaseDate())
                    .releaseCountry(musicDTO.getReleaseCountry())
                    .build();
            int i = musicMapper.updateById(music);
            if(i>0){
                Music backMusic = musicMapper.selectById(music.getId());
                if(Objects.nonNull(backMusic)){
                    ShowMusicInfo musicInfo = ShowMusicInfo.builder()
                            .id(music.getId())
                            .songName(music.getSongName())
                            .singer(music.getSinger())
                            .lyric(music.getLyric())
                            .songType(String.valueOf(music.getSongType().getCode()))
                            .picAddress(music.getPicAddress())
                            .picRealAddress(music.getPicRealAddress())
                            .songAddress(music.getSongAddress())
                            .songRealAddress(music.getSongRealAddress())
                            .totalTime(music.getTotalTime())
                            .contentType(music.getContentType())
                            .lyricist(music.getLyricist())
                            .composer(music.getComposer())
                            .arranger(music.getArranger())
                            .tempo(music.getTempo())
                            .remixer(music.getRemixer())
                            .album(music.getAlbum())
                            .albumArtist(music.getAlbumArtist())
                            .copyright(music.getCopyright())
                            .format(music.getFormat())
                            .releaseDate(music.getReleaseDate())
                            .releaseCountry(music.getReleaseCountry())
                            .build();
                    return ResultUtil.getResponseResult(HttpStatus.OK.value(), "更新成功！",musicInfo);
                }
                return ResultUtil.getResponseResult(HttpStatus.OK.value(), "没有查询到任何数据！",null);
            }
            return ResultUtil.getResponseResult(HttpStatus.OK.value(), "更新失败!",null);
        }
        return ResultUtil.getResponseResult(HttpStatus.OK.value(), "异常!",null);
    }

    @Override
    public Result findShowMusic(QueryAllMusicParam queryAllMusicParam) {
        Integer currentPage = queryAllMusicParam.getCurrentPage();
        String keywords = queryAllMusicParam.getKeywords();
        Page<Music> page=new Page<>(currentPage,10);
        Page<Music> musicPage;
        if(Objects.nonNull(keywords)){
            LambdaQueryWrapper<Music> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.like(Music::getSongName,keywords).or().like(Music::getSinger,keywords);
            musicPage = musicMapper.selectPage(page, queryWrapper);
        }else{
           musicPage = musicMapper.selectPage(page, null);
        }
        List<Music> musicList = musicPage.getRecords();
        long total = musicPage.getTotal();
        if(Objects.nonNull(musicList)){
            List<MusicShowVO> list=new ArrayList<>();
            musicList.forEach(music -> {
                MusicShowVO vo = MusicShowVO.builder()
                        .id(music.getId())
                        .name(music.getSongName())
                        .url(music.getSongAddress())
                        .singer(music.getSinger())
                        .lyric(music.getLyric())
                        .picUrl(music.getPicAddress())
                        .totalTime(music.getTotalTime())
                        .songType(music.getSongType().getCode())
                        .contentType(music.getContentType())
                        .lyricist(music.getLyricist())
                        .composer(music.getComposer())
                        .arranger(music.getArranger())
                        .tempo(music.getTempo())
                        .remixer(music.getRemixer())
                        .album(music.getAlbum())
                        .copyright(music.getCopyright())
                        .albumArtist(music.getAlbumArtist())
                        .releaseDate(music.getReleaseDate())
                        .releaseCountry(music.getReleaseCountry())
                        .build();
                list.add(vo);
            });
            MusicShowInfoVO infoVO = MusicShowInfoVO.builder().musicShowVO(list).total(total).build();
            return ResultUtil.getResponseResult(HttpStatus.OK.value(),"查询成功！",infoVO);
        }
        return ResultUtil.getResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "查询失败！",null);
    }
}
